<?php

if(!defined('IN_ECS'))
{
	die('Hacking attempt');
}

define('APPNAME', 'ECSHOP');
define('VERSION','v2.7.2');
define('RELEASE','20110604');


class ECS
{
	var $db_name = '';
	var $prefix = 'ecs_';
	
	function ECS($db_name,$prefix)
	{
		$this->db_name = $db_name;
		$this->prefix = $prefix;
	}
	
	function table($str)							//拼接数据库表名
	{
		return '`' . $this->db_name .'`.`' . $this->prefix . $str . '`';
	}
	
	function data_dir($sid = 0)							//数据备份路径
	{
		if(empty($sid))
		{
			$s = 'data';
		}else 
		{
			$s = 'user_files/';
			$s .= ceil($sid/1000) . '/';
			$s .= $sid % 3000;
		}
		return $s;
	}
	
	function image_dir($sid = 0)						//图片路径
	{
		if(empty($sid))
		{
			$s = 'images';
		}else 
		{
			$s = 'user_files/';
			$s .= ceil($sid/1000) . '/';
			$s .= $sid % 3000 .'/';
			$s .= 'images';
		}
		return $s;
	}
	
	function url()
	{
		$curr = strpos(PHP_SELF, ADMIN_PATH . '/')!== FALSE ? preg_replace('/(.*)('.ADMIN_PATH.')(\/?)(.)*/i', '\1', dirname(PHP_SELF)) : dirname(PHP_SELF);
		$root = str_replace('\\', '/', $curr);
		
		if(substr($root, -1)!= '/')
		{
			$root .= '/';
		}
		
		return $this->get_domain().$root;
		
	}
	
	function get_domain()
	{
		$protocol = $this->http();
		
		if(isset($_SERVER['HTTP_X_FORWARDED_HOST']))
		{
			$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
		}
		elseif(isset($_SERVER['HTTP_HOST']))
		{
			$host = $_SERVER['HTTP_HOST'];
		}
		else 
		{
			if(isset($_SERVER['SERVER_PORT']))
			{
				$port = ':' . $_SERVER['SERVER_PORT'];
				
				if((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol))
				{
					$port = '';
				}
			}
			else 
			{
				$port = '';
			}
			
			if(isset($_SERVER['SERVER_NAME']))
			{
				$host = $_SERVER['SERVER_NAME'] . $port;
			}
			elseif(isset($_SERVER['SERVER_ADDR']))
			{
				$host = $_SERVER['SERVER_ADDR'] . $port;
			}
		}
		return  $protocol.$host;
	}
	
	function http()
	{
		return (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';
	}
	
	
}






